/**
 * @name plugin suggestion_obj unit
 * @copyright	tcsos	inc
 * @author	周琪
 * @version	v1.0
 */
(function(undefined){
	var suggestion_obj = {};	   
	
	//input-text
	suggestion_obj.input = '';		
	
	//显示框
	suggestion_obj.showbox = '';
	
	//当前KEY
	suggestion_obj.curkey = 0;
	
	//停止获取数据
	suggestion_obj.stoprequest = false;
	
	//是否已经关闭BOX
	suggestion_obj.is_close = false;
	
	//是否已经隐藏
	suggestion_obj.is_hide = false;
	
	//鼠标选择
	suggestion_obj.mouse_selected = false;
	
	//偏移量
	suggestion_obj.offset = {
		l:0,
		t:0
	};
	
	//数据脚本
	suggestion_obj.data_script = '';
	
	//尺寸大小
	suggestion_obj.size = {
		w:100,
		h:100
	}	
	
	//初始化
	suggestion_obj.init = function(param) {
		if(param.size != undefined) {
			suggestion_obj.size = param.size;
		}
		
		if(param.offset != undefined) {
			suggestion_obj.offset = param.offset; 			
		}
        
        if(param.url != undefined){
            suggestion_obj.url = param.url;
        }
		
		suggestion_obj.input = param.obj;
        
        //样式初始化
        suggestion_obj.style();
		
		//自动提示关闭
		suggestion_obj.input.attr('autocomplete','off');
		
		//绑定事件		
		suggestion_obj.input.bind('keydown', suggestion_obj.event.keydown);
		suggestion_obj.input.bind('keyup', suggestion_obj.event.keyup);
		suggestion_obj.input.bind('blur', suggestion_obj.event.blur);			
	}
            
    //初始化样式
    suggestion_obj.style = function() {
        common_fun_obj.append_style(config.path.plugin + 'suggestion/main.css?v=' + config.version);        
    }
	
	//事件
	suggestion_obj.event = {
		//键盘按下
		keydown:function(e) {			
			var e = e || window.event;
			
			if(suggestion_obj.is_close) {
				return;
			}            
			
			switch (e.keyCode) {
                case 38:				
					if (suggestion_obj.is_hide) {
						if (this.value == '') {
							return;
						}
						suggestion_obj.showbox.show();
						suggestion_obj.is_hide = false;
					} else {
						suggestion_obj.curkey--;
					}
					suggestion_obj.unit.select_item();
                	break;					
                case 40:
					if (suggestion_obj.is_hide) {
						if (this.value == '') {
							return;
						}
						suggestion_obj.showbox.show();
						suggestion_obj.is_hide = false;
					} else {
						suggestion_obj.curkey++;						
					}										
					suggestion_obj.unit.select_item();
                	break;                                
				case 13:
					suggestion_obj.is_hide = true;					
					break;					
                default:
					suggestion_obj.stoprequest = false;					
                    break;
            }
		},
		//键盘放开
		keyup:function(e) {
			var e = e || window.event;
			
			if(suggestion_obj.is_close) {
				return;
			}																								         
			
			switch(e.keyCode) {
				case 38:
					suggestion_obj.stoprequest = true;
					break;
				case 40:
					suggestion_obj.stoprequest = true;									
					break;
				case 13:
					suggestion_obj.unit.hide();
					break;										
				default:																						
					if(this.value != '') {											
						suggestion_obj.request();
					}
					break;
			}
		},
		//列表点击
		list_click:function(e) {			
			var e = e || window.event, target = e.target || e.srcElement;			           
			suggestion_obj.unit.hide();			                   
			suggestion_obj.input.val(this.innerHTML);                         		
		},
		//列表获得鼠标
		list_mouseover:function(e) {
			var items = suggestion_obj.showbox.find('li'); 					
        	for (var i = 0, len = items.length; i < len; i++) {
            	items[i].className = '';
			}
            
			this.className = "hover";
			
			suggestion_obj.curkey = parseInt(this.getAttribute("key"));
			            
			this.onmouseout = function() {
				this.className = '';				
			}
			
			suggestion_obj.mouse_selected = true;			                	
		},
		//列表鼠标离开
		list_mouseout: function() {
			suggestion_obj.mouse_selected = false;
		},
		//离开焦点
		blur:function() {			
			if (!suggestion_obj.mouse_selected) {
				suggestion_obj.unit.hide();
			}			
		}
	}
	
	suggestion_obj.unit = {
		//隐藏
		hide:function() {
			if (suggestion_obj.showbox) {
				suggestion_obj.showbox.hide();				
				suggestion_obj.is_hide = true;													
			}			
		},
		//选择某项
		select_item:function() {
			var items = suggestion_obj.showbox.find('li');
			if(items.length < 1) {
				return;				
			}			
			if (suggestion_obj.curkey < 0) {
                suggestion_obj.curkey = items.length - 1;
            } else if(suggestion_obj.curkey >= items.length) {
				suggestion_obj.curkey = 0;				
			}
			
			for (var i = 0, len = items.length; i < len; i++) {
                items[i].className = "";
            }
			items[suggestion_obj.curkey].className = 'hover';
			suggestion_obj.input.val(items[suggestion_obj.curkey].innerHTML);
			suggestion_obj.stoprequest = true;
		},
		//显示列表
		show:function(data) {
			if (typeof(data) != 'object' || typeof(data) == 'undefined') {
				return false;
			} 
			                         
			//如果boxlist不存在就创建
			if(!suggestion_obj.showbox) {
				suggestion_obj.showbox = $('<div></div>');
                suggestion_obj.showbox.attr('class', 'suggestion');															
                suggestion_obj.showbox.css('width', suggestion_obj.size.w + 'px');
				suggestion_obj.showbox.show();
				
				$(document.body).append(suggestion_obj.showbox);
				
				var offset = suggestion_obj.input.offset();
															
				suggestion_obj.showbox.css('left', (offset.left + suggestion_obj.offset.l) + 'px') ;
				suggestion_obj.showbox.css('top', (offset.top + suggestion_obj.offset.t) + 'px') ;								
            }

			var html = '<ul style="height:' + suggestion_obj.size.h + 'px;overflow:hidden;">';			
			for(i = 0, c = data.length; i < c; i++) {
				html += '<li key="' + i + '" style="overflow:hidden;">' + data[i] + '</li>';				
			}
			
			html += '</ul><div class="close"><a href="javascript:;">关闭</a></div>';			
			suggestion_obj.showbox.html(html);
			
			suggestion_obj.curkey = -1;
			
			//不隐藏
			suggestion_obj.is_hide = false;
			
			//绑定关闭事件
			suggestion_obj.showbox.find('a').bind('click', suggestion_obj.unit.close);
			suggestion_obj.showbox.find('a').bind({
				mouseover:function() {
					suggestion_obj.mouse_selected = false;
				},
				mousemove:function() {
					suggestion_obj.mouse_selected = true;
				}				
			});
			
			//显示列表
			suggestion_obj.showbox.show();
			
			//激活鼠标句柄
			suggestion_obj.unit.mouse_handle();			               		
		},
		//关闭
		close:function() {					
			suggestion_obj.input.attr('autocomplete','on');
			suggestion_obj.input.focus();
			suggestion_obj.unit.hide();
			suggestion_obj.is_close = true;					
		},
		//鼠标句柄
		mouse_handle:function() {
			suggestion_obj.showbox.find('li').bind('click', suggestion_obj.event.list_click);
			suggestion_obj.showbox.find('li').bind('mouseover', suggestion_obj.event.list_mouseover);
			suggestion_obj.showbox.find('li').bind('mouseout', suggestion_obj.event.list_mouseout);								
		}		
	}	
	
	//读取数据
	suggestion_obj.request = function(){
		var head = document.getElementsByTagName("head")[0];			
		if(suggestion_obj.stoprequest) {
			return;
		}
		
		if(!config.browse.ie) {
			if(suggestion_obj.data_script) {
				head.removeChild(suggestion_obj.data_script);				
			}
			
			suggestion_obj.data_script = null;			
		}
		
		if(!suggestion_obj.data_script) {
			var script = document.createElement("script");
            script.type = "text/javascript";
            script.charset = "gb2312";						
			head.insertBefore(script, head.firstChild);
            suggestion_obj.data_script = script;
		}
		
		var rd = new Date().getTime();
		var key = encodeURIComponent(suggestion_obj.input.val());
        if(suggestion_obj.url){
            var url = suggestion_obj.url + key + "&rd=" + rd;
        }else{
            var url = "http://suggestion.baidu.com/su?wd=" + key + "&rd=" + rd;
        }
        
		suggestion_obj.data_script.src = url;		
     }
     
     suggestion_obj.sug = function(callobj) {
         if(typeof(callobj) != 'object' || typeof(callobj.s) == 'undefined' || typeof(callobj.s[0]) == 'undefined') {
            suggestion_obj.unit.hide();
            return false;                        
        }
        
        suggestion_obj.unit.show(callobj.s);                    
     }		
    
    if(window.suggestion_obj == undefined) {		
        window.suggestion_obj = suggestion_obj;
    }
}());

(function(undefined){
	var baidu_obj = {};
	
	//百度自动完成	
	baidu_obj.sug = function(callobj) {
		if(typeof(callobj) != 'object' || typeof(callobj.s) == 'undefined' || typeof(callobj.s[0]) == 'undefined') {
			suggestion_obj.unit.hide();
			return false;						
		}
		
		suggestion_obj.unit.show(callobj.s);					
	}
	
	if(window.baidu == undefined) {
		window.baidu = baidu_obj;
	}	
})();
